package com.hnqcgczy.sgzshop.dao.impl;
import com.hnqcgczy.sgzshop.dao.IUserDao;
import com.hnqcgczy.sgzshop.dbc.MyTransaction;
import com.hnqcgczy.sgzshop.vo.TUser;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements IUserDao {
    //组合   两个类
    private MyTransaction myTransaction;//X

    public UserDaoImpl(MyTransaction myTransaction) {
        this.myTransaction = myTransaction;//赋值
    }

    public static void main(String[] args) {
        MyTransaction myTransaction=new MyTransaction(true);
        UserDaoImpl userDao = new UserDaoImpl(myTransaction);
        try {
            boolean b = userDao.login("admin", "admin");
            System.out.println(b);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    @Override
    public boolean login(String tu_id, String tu_password) throws SQLException {
        //获取数据库连接
        Connection conn = myTransaction.getConn();
        //PreparedStatement向数据库中发送sql语句
        String sql="select * from t_user where tu_id=? and tu_password=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        //给sql语句 中的？注入值
        ps.setString(1,tu_id);
        ps.setString(2,tu_password);
        //执行sql语句,得到结果集
        ResultSet rs = ps.executeQuery();
        boolean flag=false;
        while (rs.next()){
            flag=true;
        }
        //释放资源
        if(rs!=null){
            rs.close();
        }
        if(ps!=null){
            ps.close();
        }
        //关闭数据库  在业务层进行
        return flag;
    }

    @Override
    public boolean insert(String tu_id, String tu_password) throws SQLException {
        //获取数据库连接
        Connection conn = myTransaction.getConn();
        //PreparedStatement向数据库中发送sql语句
        String sql="insert into t_user(tu_id,tu_password) values(?,?) ";
        PreparedStatement ps = conn.prepareStatement(sql);
        //给sql语句 中的？注入值
        ps.setString(1,tu_id);
        ps.setString(2,tu_password);
        //执行sql语句
        int i = ps.executeUpdate();
        if(ps!=null){
            ps.close();
        }
        return i>0;
    }


    @Override
    public List<TUser> getAll() throws SQLException {
        return null;
    }

    @Override
    public TUser getUserByID(String tu_id) throws SQLException {

        return null;
    }

    @Override
    public List<TUser> getUsersByName(String tu_name) throws SQLException {
        return null;
    }

    @Override
    public List<TUser> getUserByAny(Object... obj) throws SQLException {
        return null;
    }

    @Override
    public boolean update(TUser tUser) throws SQLException {
        return false;
    }

    @Override
    public boolean deleteById(String tu_id) throws SQLException {
        return false;
    }

    @Override
    public boolean queryByID(String tu_id) throws SQLException {
        //????  查昵称可不可用
        Connection conn = myTransaction.getConn();
        String sql="select * from t_user where tu_id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,tu_id);
        ResultSet rs = ps.executeQuery();
        boolean flag=false;
        while (rs.next()){
            flag=true;
        }
        //释放资源
        if(rs!=null){
            rs.close();
        }
        if(ps!=null){
            ps.close();
        }
        return flag;
    }
}
